package com.larly.controller;

import com.larly.annotation.AuthCheck;
import com.larly.common.BaseResponse;
import com.larly.common.PageRequest;
import com.larly.common.ResultUtils;
import com.larly.model.domain.User;
import com.larly.model.dto.privatemessage.PrivateMessageCreateRequest;
import com.larly.model.dto.privatemessage.PrivateMessageSelectRequest;
import com.larly.model.vo.PrivateMessageVO;

import com.larly.service.PrivateMessageService;
import com.larly.service.UserService;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/private-message")
public class PrivateMessageController {

    @Resource
    private PrivateMessageService privateMessageService;

    @Resource
    private UserService userService;

    /**
     * 发送私信（管理员发送不走 websocket）
     *
     *
     * @param dto      私信内容
     */
    @PostMapping("/send")
    @AuthCheck(requiredRoles = {"admin"})
    public BaseResponse<Boolean> sendPrivateMessage(
            HttpServletRequest request,
            @Valid @RequestBody PrivateMessageCreateRequest dto) {
        User loginUser = userService.getLoginUser(request);
        boolean success = privateMessageService.createPrivateMessage(dto, loginUser.getId());
        return ResultUtils.success(success);
    }

    /**
     * 查询会话历史消息（展示两个用户完整的聊天记录）
     */
    @GetMapping("/history")
    public BaseResponse<List<PrivateMessageVO>> getConversationHistory(HttpServletRequest request, @RequestBody PrivateMessageSelectRequest dto) {
        PageRequest pageRequest = new PageRequest();
        pageRequest.setPageNum(dto.getPageNum());
        pageRequest.setPageSize(dto.getPageSize());
        User loginUser = userService.getLoginUser(request);
        List<PrivateMessageVO> privateMessageVOS = privateMessageService.queryHistory(pageRequest, loginUser.getId(), dto.getReceiverId());
        return ResultUtils.success(privateMessageVOS);
    }

    /**
     * 获取会话列表(类似微信的会话列表界面)
     */
    @PostMapping("/conversation-list")
    public BaseResponse<List<PrivateMessageVO>> getConversationList(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        List<PrivateMessageVO> list = privateMessageService.queryLatestList(loginUser.getId());
        return ResultUtils.success(list);
    }

    /**
     * 标记私信为已读
     */
    @PutMapping("/mark-read")
    public BaseResponse<Boolean> markAsRead(@RequestParam Integer senderId, @RequestParam Integer currentUserId) {
        boolean success = privateMessageService.readPrivateMessage(senderId, currentUserId);
        return ResultUtils.success(success);
    }

    /**
     * 查询未读消息数量
     */
    @GetMapping("/unread-count")
    public BaseResponse<Long> countUnread(@RequestParam Integer currentUserId, @RequestParam Integer targetUserId) {
        long count = privateMessageService.countUnreadByTarget(currentUserId, targetUserId);
        return ResultUtils.success(count);
    }
}